#include "huabiwidget.h"
#include "ui_huabiwidget.h"
#include <QPainter>
#include <QBitmap>
#include <QStyleOption>
#include <QtDebug>
#include <QToolButton>
HuaBiWidget::HuaBiWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HuaBiWidget)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);// 设置窗口标志，使其表现为弹出窗口，并且无边框

    QList<QToolButton*> allButtons = ui->widget_huabi->findChildren<QToolButton*>();
    // 给所有按钮绑定点击事件
    for (QToolButton* button : allButtons) {
        QObject::connect(button, &QToolButton::clicked, [this, button]() {
            qDebug() << "Button clicked: " << button->text();
            close();
            emit sonOfHuabiIsSelected(button->text());
        });
    }

}

HuaBiWidget::~HuaBiWidget()
{
    delete ui;
}

void HuaBiWidget::paintEvent(QPaintEvent *event)
{
    //绘制样式
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);//绘制样式
    QBitmap bmp(this->size());
    bmp.fill();
    QPainter painter(&bmp);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::black);
    painter.setRenderHint(QPainter::Antialiasing);
    //设置边框为圆角12px
    painter.drawRoundedRect(bmp.rect(), 12, 12);
    setMask(bmp);
    //再画颜色块
    QRect tmpRect = QRect(0, 0, this->width(), this->height());
    QBrush brush = QBrush(QColor("#ffffff"));
    p.setPen(Qt::NoPen);  //去掉边框线
    p.setBrush(brush);
    p.drawRect(tmpRect);

}

QList<QToolButton *> HuaBiWidget::getHuabiButtons()
{
    QList<QToolButton*> buttonList;
    // 查找 widget_4 下的所有 QToolButton
    QList<QToolButton*> allButtons = ui->widget_huabi->findChildren<QToolButton*>();
    // 将 QToolButton 添加到列表中
    for (QToolButton* button : allButtons) {
        buttonList.append(button);
    }
    qDebug() << "row 190, num of huabiButton is " << buttonList.size();

    return buttonList;
}
